package com.lanyou.esb.cook.proxy.redis.proxy.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.exceptions.JedisException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.cook.proxy.common.Constants4Redis;
import com.lanyou.esb.cook.proxy.redis.JedisPoolUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.base.BaseStrJedisProxy;

/**
 * 告警重复次数
 * 
 * @author Davey.wu
 */
public class IfAlarmRepeatTimesJedisProxy extends BaseStrJedisProxy<Long> {
	public static final String KEYS_SUFFIX = Constants4Redis.PREFIX_REPEATTIME;

	public IfAlarmRepeatTimesJedisProxy() {
		this.setTimeOut(Constants4Redis.REDIS_TIMEOUT_IFALARMRPT);
	}

	@Override
	public String getKey(String field) {
		return KEYS_SUFFIX + KEY_SEPARATOR + field;
	}

	@Override
	public String getKeyFromFields(String... field) {
		if (field == null) {
			return KEYS_SUFFIX;
		}
		String key = KEYS_SUFFIX;
		for (String str : field) {
			key += KEY_SEPARATOR + str;
		}
		return key;
	}

	@Override
	public String getKeyFromObject(Long target) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Long get(String key) throws JedisException, JsonParseException,
			JsonMappingException, IOException {
		String result = "";
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			result = jedis.get(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		try {
			return Long.parseLong(result);
		} catch (Exception e) {
			return 0l;
		}
	}

	@Override
	public long setex(String key, Long value) throws JedisException,
			JsonProcessingException {
		if (value == null) {
			return 0;
		}
		long result = 0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			jedis.expire(key, this.getTimeOut());
			result = jedis.incrBy(key, value.longValue());
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		return result;
	}

	public Map<String, Long> getMap(List<String> keys)
			throws JsonParseException, JsonMappingException, IOException {
		if (keys == null || keys.isEmpty()) {
			return null;
		}
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		Map<String, Response<String>> responses = new HashMap<>(keys.size());
		try {
			Pipeline pipe = jedis.pipelined();
			for (String key : keys) {
				responses.put(key, pipe.get(key));
			}
			pipe.sync();
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (responses.isEmpty()) {
			return null;
		}
		Map<String, Long> result = new HashMap<>(responses.size());
		for (Entry<String, Response<String>> entry : responses.entrySet()) {
			long value = 0l;
			if (StringUtils.isBlank(entry.getValue().get())) {
				continue;
			}
			try {
				value = Long.parseLong(entry.getValue().get());
			} catch (Exception e) {
				value = 0l;
			}
			result.put(entry.getKey(), value);
		}
		return result;
	}
}
